package com.code.test.second.link;

/**
 * 206
 * 题意：反转一个单链表。
 *
 * 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
 */
public class SCode206 {
    public static void main(String[] args) {
        SNode head = new SNode(1);
        SNode second = new SNode(2);
        SNode third = new SNode(3);
        SNode n4 = new SNode(4);
        SNode n5 = new SNode(5);
        SNode n6 = new SNode(6);
        SNode n7 = new SNode(7);

        head.setNext(second);
        second.setNext(third);
        third.setNext(n4);
        n4.setNext(n5);
        n5.setNext(n6);
        n6.setNext(n7);

        reverse(head);

        int a=2;
    }

    public static void reverse(SNode root) {
        //不能借助新的存储空间，只需要翻转指针

        SNode pre = null;
        SNode cur = root;
        while(cur!=null){
            //暂存下一个
            SNode tmp = cur.next;
            //当前的下一个是前一个
            cur.next = pre;
            //当前这个变成前一个，因为要往下移动，
            pre = cur;
            //更新当前这个，为之前暂存的下一个，往下推进
            cur = tmp;
        }



    }
}
